<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{$title}</title>
{webcontrol type='LoadJsCss' src="Resource/Script/jquery.js"}
{webcontrol type='LoadJsCss' src="Resource/Script/Calendar/WdatePicker.js"}
{webcontrol type='LoadJsCss' src="Resource/Css/EditCommon.css"}
{webcontrol type='LoadJsCss' src="Resource/Script/jquery.validate.js"}
{webcontrol type='LoadJsCss' src="Resource/Css/validate.css"}
{webcontrol type='LoadJsCss' src="Resource/Script/jquery.json.js"}
{webcontrol type='LoadJsCss' src="Resource/Script/common.js"}
{webcontrol type='LoadJsCss' src="Resource/Script/direct.js"}
{webcontrol type='LoadJsCss' src="Resource/Script/autocomplete/autocomplete.js"}
{webcontrol type='LoadJsCss' src="Resource/Script/autocomplete/autocomplete.css"}
{literal}

<script language="javascript">
var _tbl = null;
var _tr = null;
$(function(){
    _tbl = document.getElementById('tableList');
    _tr = _tbl.rows[_tbl.rows.length-1].cloneNode(true);

    $('#form1').validate({
        rules:{
            'rukuDate':"required"
        },
        messages:{
            'rukuDate':"日期必选"
       },
        submitHandler : function(form){
            $('#Submit').attr('disabled',true);
            form.submit();
        }
        ,onfocusout:false
        ,onclick:false
        ,onkeyup:false
        ,showErrors:function(errorMap,errorList){
            //dump(errorMap);
            var t = '';
            for(var i=0;errorList[i];i++) {
                t +=errorList[i].message+"\r";
            }

            if(t!='') {
                alert(t);
            }
        }
    });
    //设置方向键,方法一包装在jquery 中
    var name=['ganghao[]','dengji[]','cntJian[]','unit[]','cnt[]','memoView[]'];
    //*******direct(true)表示聚焦到下一个焦点时候，会选中里面的内容,false或没有参数则表示不选中
    direct({
        cellname:name,
        selectedorfocus:true,
        optionfocus:true
    });

    //复制
    $('.btnCopy').live('click',function(){
        var trs = $('.trPro');
        var tr = $(this).parents('.trPro');
        //倒数第一个clientHuaxing不为空的行,其后插入
        var pos = trs.length-1;
        for(var i=pos;i>-1;i--) {
            //如果纱支经纬都不为空,after插入
            var hx  = $('[name="ganghao[]"]',trs[i]).val();
            if(hx!='') {
                pos=i;
                break;
            }
        }
        var newTr = tr.clone(true);

        //干净的缸号html代码，没有渲染过autocomplete事件的html
        var _gHtml = $(_tr).find('[name="ganghao[]"]').parent().html();
        $('[name="ganghao[]"]',newTr).replaceWith(_gHtml);
        $(trs[pos]).after(newTr);

        //重新渲染
        autoComplete(newTr);
        $('[name="sonId[]"]',newTr).val('');
        $('[name="cnt[]"]',newTr).val('');
        $('[name="cntJian[]"]',newTr).val('');
        //赋值原来的值
        $('[name="ganghao[]"]',newTr).val($('[name="ganghao[]"]',tr).val());
        return;
    });


    //删除按钮
    $('.btnDel').live('click',function(){
        var tr = $(this).parents('.trPro');
        var id = $('[name="sonId[]"]',tr).val();
        var objs = document.getElementsByName('btnDel');
        if(objs.length==1) {
            alert('请至少保留一个产品明细!');
            return false;
        }
        if(!confirm('确认删除订单明细吗?')) return false;

        if(!id) tr.remove();
        else {
            var url='?controller=Cangku_Chengpin_Cprk&action=removeByAjax';
            var param={id:id}
            $.getJSON(url,param,function(json){
                if(!json.success) {
                    alert(json.msg);
                    return false;
                }
                tr.remove();
            });
        }

    });
    //码单事件，支持弹出选择
    $('[name="madan_btn"]').live('click',function(){
        //url地址
        var url="?controller=Cangku_Chengpin_Cprk&action=ViewMadan";
        var trRow = $(this).parents(".trPro");
        //弹出窗口，设置宽度高度
        var width = screen.width;
        var height = screen.height;
        width = width>1000?1000:width;
        height = height>550?550:height;
        //获取码单选择信息
        var madan = $('[name="Madan[]"]',trRow).val();
        var lot = $('[name="lot[]"]',trRow).val();
        window.returnValue=null;
        var ret = window.showModalDialog(url,{data:madan,lot:lot},'dialogWidth:'+width+'px;dialogHeight:'+height+'px;');

        if(!ret){
            ret=window.returnValue;
        }
        if(!ret) return;
        if(ret.ok!=1)return false;
        // dump(ret);
        $('[name="cntJian[]"]',trRow).val(ret.cntJian);
        $('[name="cnt[]"]',trRow).val(ret.cnt);
        $('[name="Madan[]"]',trRow).val(ret.data);
        $('[name="cnt[]"]',trRow).change();

        if(ret.cnt>0){
            $('[name="cnt[]"]',trRow).attr('readonly',true);
            $('[name="cntJian[]"]',trRow).attr('readonly',true);
        }else{
            $('[name="cnt[]"]',trRow).attr('readonly',false);
            $('[name="cntJian[]"]',trRow).attr('readonly',false);
        }

    });
    //禁止回车键提交
    $('#form1').keydown(function(e){
        if(e.keyCode==13){
            if(e.target.type!='textarea')event.returnValue=false;
        }
    });

    //自动控件渲染初始化
    for (var i = 0; _tbl.rows[i]; i++) {
        autoComplete(_tbl.rows[i]);
    };
});

/**
 * 添加行
*/
function addRow() {
    var t= null;
    var trs = $('.trPro');
    var tr = trs[trs.length-1];
    for(var i=0;i<5;i++) {
        var newTr = $(_tr).clone(true);
        $(tr).after(newTr);

        $('[name="sonId[]"]',newTr).val('');
        //$('[name="ganghao[]"]',newTr).val('');
        $('[name="proCode[]"]',newTr).val('');
        $('[name="guige[]"]',newTr).val('');
        $('[name="proName[]"]',newTr).val('');
        $('[name="menfu[]"]',newTr).val('');
        $('[name="color[]"]',newTr).val('');
        $('[name="kezhong[]"]',newTr).val('');
        $('[name="kouhao[]"]',newTr).val('');
        //$('[name="weimi[]"]',newTr).val('');
        $('[name="cnt[]"]',newTr).val('');
        $('[name="cntJian[]"]',newTr).val('');
        $('[name="memoView[]"]',newTr).val('');
        $('[name="unit[]"]',newTr).val('');
        $('[name="dengji[]"]',newTr).val('');

        autoComplete(newTr);
    }
}

/**
 * 自动控件
 * Time：2015/04/07 13:53:07
 * @author li
*/
function autoComplete(o){
    $('[name="ganghao[]"]',o).autocomplete('?controller=Cangku_Jingsha_Chuku&action=GetganghaoAndOrder', {
            minChars:1,
            remoteDataType:'json',
            useCache:false,
            sortResults:false,
            onItemSelect:function(v){
                var currentTr=$('[name="ganghao[]"]',o).parents('.trPro');

                var _data = v.data[0];

                $('[name="proCode[]"]',currentTr).val(_data.proCode);
                //$('[name="weimi[]"]',currentTr).val(_data.pibuWeimi);
                $('[name="menfu[]"]',currentTr).val(_data.menfu);
                $('[name="kezhong[]"]',currentTr).val(_data.kezhong);
                $('[name="guige[]"]',currentTr).val(_data.guige);
                $('[name="proName[]"]',currentTr).val(_data.proName);
                $('[name="kouhao[]"]',currentTr).val(_data.kouhao);
                $('[name="color[]"]',currentTr).val(_data.color);
            }
    });
}

</script>
<style type="text/css">
    .trPro td{
        white-space: nowrap;
        padding: 1px 3px 1px 3px;
    }
    .readonly{
        background-color: #dedede;
    }
</style>
{/literal}
</head>
<body>
<form name="form1" id="form1" action="{url controller=$smarty.get.controller action='save'}" method="post">
<fieldset>
<legend>基本信息</legend>
<table id="mainTable">
<tr>
    <td class="title">入库单号：</td>
    <td>
        <input name="rukuCode" type="text" id="rukuCode" value="{$aRow.rukuCode|default:'系统自动生成'}" readonly="true">
    </td>
    <td class="title">入库日期：</td>
    <td><input name="rukuDate" type="text" id="rukuDate" value="{$aRow.rukuDate|default:$smarty.now|date_format:'%Y-%m-%d'}" onClick="calendarUntilNow()"></td>
    <td class="title">入库类型：</td>
    <td><input name="kind" type="text" id="kind" value="{$aRow.kind|default:'验布入库'}"  readonly="true"></td>
</tr>
<tr>
    <td class="title">送货号：</td>
    <td><input name="songhuoCode" id="songhuoCode" value="{$aRow.songhuoCode}"></td>
    <td class="title"></td>
    <td></td>
</tr>
<tr>
    <td class="title">备注：</td>
    <td colspan="5"><textarea name="memo" id="memo" style="width:96%;height:70px;">{$aRow.memo}</textarea></td>
</tr>
</table>
</fieldset>

<div style="max-width:100%; max-height:300px;width:100%;overflow-x:auto;padding-bottom:30px; border:1px solid #999;margin-top:10px;">
<table id="tableList" style="width:100%;text-align:center;" cellpadding="0" cellspacing="0">
  <tr style="background-color:#D4E2F4; font-size:12px; height:28px;">
    <td><!-- [ <a href='javascript:void(0)' onClick="addRow()">+5行</a> ] -->
    </td>
    <td>生产序号</td>
    <td>产品编号</td>
    <td>品名规格</td>
    <td>经纬密</td>
    <td>颜色</td>
    <td>克重</td>
    <td>门幅</td>
    <td>等级</td>
    <td>件数</td>
    <td>单位</td>
    <td>数量</td>
    {if $aRow.id!=""}
    <td>码单</td>
    {/if}
    <td>说明</td>
    </tr>
    {foreach from=$aRow.Products item=item}
  <tr class='trPro'>
     <td class="editCol">
      <a href='javascript:void(0)' class="btnDel" name="btnDel" id="btnDel" ><img src='Resource/Image/toolbar/delete.gif' border="0" title='删除行'/></a>
      <input name="rukuId[]" type="hidden" id="rukuId[]" value="{$item.rukuId}">
      <input name="sonId[]" type="hidden" id="sonId[]" value="{$item.id}">
     </td>
    <td><input name="orderCode[]" type="text" readonly id="orderCode[]" value="{$item.orderCode}" size="6" class="readonly">
        <input name="ord2proId[]" type="hidden" readonly id="ord2proId[]" value="{$item.ord2proId}">
        <input name="orderId[]" type="hidden" readonly id="orderId[]" value="{$item.orderId}">
    </td>
    <td>
        <input name="proCode[]" type="text" readonly id="proCode[]" value="{$item.proCode}" size="6" class="readonly">
        <input name="productId[]" type="hidden" readonly id="productId[]" value="{$item.productId}">
    </td>
    <td><input name="guige[]" type="text" id="guige[]" readonly value="{$item.guige}" size="14" class="readonly"></td>
    <td><input name="jingwei[]" type="text" readonly id="jingwei[]" value="{$item.jingwei}" size="10" class="readonly"></td>
    <td><input name="color[]" type="text" id="color[]" readonly value="{$item.color}" size="14" class="readonly"></td>
    <td><input name="kezhong[]" type="text" id="kezhong[]" value="{$item.kezhong}" size="6" readonly class="readonly"></td>
    <td><input name="menfu[]" type="text" id="menfu[]" value="{$item.menfu}" size="6" readonly class="readonly"></td>
    <td><select name="dengji[]" id="dengji[]" style="width:auto">
        <option value="">请选择</option>
        <option value="一等品" {if $item.dengji=='一等品'}selected{/if}>一等品</option>
        <option value="二等品" {if $item.dengji=='二等品'}selected{/if}>二等品</option>
        <option value="等外品" {if $item.dengji=='等外品'}selected{/if}>等外品</option>
        </select></td>
    <td><input name="cntJian[]" type="text" id="cntJian[]" value="{$item.cntJian}" size="3" readonly class="readonly"></td>
    <td><select name="unit[]" id="unit[]" style="width:auto">
        <option value="">请选择</option>
        <option value="M" {if $item.unit=='M'}selected{/if}>米</option>
        <option value="Y" {if $item.unit=='Y'}selected{/if}>码</option>
        </select></td>
    <td><input name="cnt[]" type="text" id="cnt[]" value="{$item.cnt}" size="5" readonly class="readonly"></td>
    {if $aRow.id!=""}
    <td><a href='javascript:;' name='madan_btn'>码单</a><input name="Madan[]" type="hidden" id="Madan[]" value='{$item.Madan}'></td>
    {/if}
    <td><input name="memoView[]" type="text" id="memoView[]" value="{$item.memoView}" size="12">
    <input name="creater[]" type="hidden" id="creater[]" value="{$item.creater|default:$smarty.session.USERID}"></td>
    </tr>
    {/foreach}
  </table>
  </div>

<div id="footer">
<table id="buttonTable" align="center">
<tr>
    <td>
        <input name="id" type="hidden" id="id" value="{$aRow.id}">
        <input name="addTime" type="hidden" id="addTime" value="{$addTime}">
        <input name="isMadan" type="hidden" id="isMadan" value="1">
        <input name="fromAction" type="hidden" id="fromAction" value="{$smarty.get.fromAction}">
        <input type="Submit" id="Submit" name="Submit" value='确定'>
      <input type="button" id="Back" name="Back" value='取消' onClick="window.history.go(-1);">
      </td>
    </tr>
</table>
</div>
</form>
<div id='divError' name='divError' style="color:red;">
</div>
</body>
</html>
